/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class MonthlyDetailSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;
    private MonthlyDetailEntity md;
    @EJB
    private DailyDetailSessionBean ddsb;
    private SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");

    public MonthlyDetailEntity getMonthlyDetailByDate(Date d){
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String temp = sdf.format(d);
        Calendar cal = new GregorianCalendar();
        cal.setTime(d);
        int correction = cal.get(Calendar.MONTH); 
        try{
            d = sdf.parse(temp);
        }
        catch(ParseException ex){}
        cal.setTime(d);
        cal.set(Calendar.MONTH, correction);
        Timestamp ts = new Timestamp(cal.getTime().getTime());
        ts.setMinutes(1);
        Query query  = em.createQuery("SELECT u FROM MonthlyDetailEntity u where u.ts = :dd");
        query.setParameter("dd",ts);
        MonthlyDetailEntity md = null;
        try{
            md = (MonthlyDetailEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            System.out.println("Get Month No Result "+ ts.toString());
//            throw ex;
        }
        return md;
    }
    public MonthlyDetailEntity getMonthlyDetailByDateForDaily(Date d){
        d.setDate(01);
        Query query = em.createQuery("SELECT u FROM MonthlyDetailEntity u where u.date = :dd");
        query.setParameter("dd", d);        
        MonthlyDetailEntity md = null;
                try{
            md = (MonthlyDetailEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            System.out.println("No Result");
//            throw ex;
        }
        return md;
                
    }
    
    public MonthlyDetailEntity getMonthlyDetailById(Long id){
        return em.find(MonthlyDetailEntity.class,id);
    }
    
    public void addMonthlyDetail(MonthlyDetailEntity md){
        md.getTs().setTime(md.getDate().getTime());
        md.calculateTotalTime();
        em.persist(md);
    }
    
    public void updateMonthlyDetail(MonthlyDetailEntity md){
        md.calculateTotalTime();
        em.merge(md);
        em.flush();
    }
    
    public void generateAYear(int year) throws ParseException{
        Date d = null;
        Calendar cal = new GregorianCalendar();
        String s = "01/01/"+ year;
        try {
            d = formatter.parse(s);
        }
        catch (ParseException e){
            e.printStackTrace();
        }
        cal.setTime(d);
        while ((cal.getTime().getYear()+1900) == year){
                 MonthlyDetailEntity md = new MonthlyDetailEntity();
                 md.setDate(cal.getTime());
                 setDays(md);
                 addMonthlyDetail(md);
                 cal.add(Calendar.MONTH, 1);
        }
        ddsb.setMonth();
        return;
    }
    
    public void setDays(MonthlyDetailEntity md){
        Date d = md.getDate();
        Calendar cal = new GregorianCalendar();
        cal.setTime(d);
        DailyDetailEntity dd;
        Date temp = cal.getTime();
        while (temp.getMonth() == d.getMonth()){
            dd = ddsb.getDailyDetailByDate(cal.getTime());
            md.getDays().add(dd);
            cal.add(Calendar.DATE, 1);
            temp = cal.getTime();
        }
    }
    
    public void removeMonthlyDetailByDate(Date d){
        em.remove(getMonthlyDetailByDate(d));
    }

    public MonthlyDetailEntity getMd() {
        return md;
    }

    public void setMd(MonthlyDetailEntity md) {
        this.md = md;
    }
    
    public int countWorkDays(MonthlyDetailEntity md){
        return md.getDays().size();
    }
    
    public List<DailyDetailEntity> getAllDays(MonthlyDetailEntity md){
        Calendar cal = new GregorianCalendar();
        List<DailyDetailEntity> out = new ArrayList<DailyDetailEntity>();
        Date temp = cal.getTime();
        DailyDetailEntity day = new DailyDetailEntity();
        cal.setTime(md.getDate());
        while(temp.getMonth() == md.getDate().getMonth()){
            day = ddsb.getDailyDetailByDate(cal.getTime());
            out.add(day);
            cal.add(Calendar.DATE, 1);
            temp = cal.getTime();
        }
        return out;
    }
    
    public List<MonthlyDetailEntity> getAllMonths(){
        Query query  = em.createQuery("SELECT u FROM MonthlyDetailEntity u");
        List<MonthlyDetailEntity> md = new ArrayList<MonthlyDetailEntity>();
        md = query.getResultList();
        return md;
    }
    
    public void updateProduction(MonthlyDetailEntity month){
        List<DailyDetailEntity> days = month.getDays();
        month.setReservedA(0);
        month.setReservedB(0);
        month.setReservedC(0);
        month.setReservedD(0);
        month.setScheduleA(0);
        month.setScheduleB(0);
        month.setScheduleC(0);
        month.setScheduleD(0);
        int rA = 0;
        int rB = 0;
        int rC = 0;
        int rD = 0;
        int sA = 0;
        int sB = 0;
        int sC = 0;
        int sD = 0;
        for (DailyDetailEntity day : days){
//            month.setReservedA(month.getReservedA()+day.getReservedA());
//            month.setReservedB(month.getReservedB()+day.getReservedB());
//            month.setReservedC(month.getReservedC()+day.getReservedC());
//            month.setReservedD(month.getReservedD()+day.getReservedD());
//            month.setScheduleA(month.getScheduleA()+day.getScheduleA());
//            month.setScheduleB(month.getScheduleB()+day.getScheduleB());
//            month.setScheduleC(month.getScheduleC()+day.getScheduleC());
//            month.setScheduleD(month.getScheduleD()+day.getScheduleD());
//            System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
//            System.out.println("Month Id: "+month.getId());
//            System.out.println("Number of Days: "+days.size());
//            System.out.println("Day :" + day.getDate().toString());
//            System.out.println("Schedule A: "+day.getScheduleA());
//            System.out.println("Schedule B: "+day.getScheduleB());
//            System.out.println("Schedule C: "+day.getScheduleC());
//            System.out.println("Schedule D: "+day.getScheduleD());
//            System.out.println("Reserved A: "+day.getReservedA());
//            System.out.println("Reserved B: "+day.getReservedB());
//            System.out.println("Reserved C: "+day.getReservedC());
//            System.out.println("Reserved D: "+day.getReservedD());
            sA += day.getScheduleA();
            sB += day.getScheduleB();
            sC += day.getScheduleC();
            sD += day.getScheduleD();
            rA += day.getReservedA();
            rB += day.getReservedB();
            rC += day.getReservedC();
            rD += day.getReservedD();
//            System.out.println("sA: " +sA);
//            System.out.println("sB: " +sB);
//            System.out.println("sC: " +sC);
//            System.out.println("sD: " +sD);
//            System.out.println("rA: " +rA);
//            System.out.println("rB: " +rB);
//            System.out.println("rC: " +rC);
//            System.out.println("rD: " +rD);
        }
        month.setReservedA(rA);
        month.setReservedB(rB);
        month.setReservedC(rC);
        month.setReservedD(rD);
        month.setScheduleA(sA);
        month.setScheduleB(sB);
        month.setScheduleC(sC);
        month.setScheduleD(sD);
        updateMonthlyDetail(month);
    }
    
}
